package com.tanhua.sso.controller;


import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.tanhua.common.utils.NoAuthorization;

import com.tanhua.sso.service.BackstageLoginService;
import com.tanhua.sso.vo.BackgroundUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("management/system")
@Slf4j
public class BackstageLoginController {

    @Autowired
    private BackstageLoginService backstageLoginService;
    /**
     * 提供验证码
     */
    @NoAuthorization
    @GetMapping("users/verification")
    public void VerificationCode(HttpServletResponse response,@RequestParam(value = "uuid") String uuid){
        LineCaptcha lineCaptcha = backstageLoginService.VerificationCode(uuid);
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
        } catch (Exception e) {
            log.error("发送验证码出错"+e);
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                log.error("发送验证码关流出错"+e);
            }
        }
    }


    /**
     * 用户登录,传token
     * @param jsonParam
     * @return
     */
    @PostMapping("users/login")
    public ResponseEntity<Object> userLogin(@RequestBody JSONObject jsonParam){
        try {
            String token = backstageLoginService.userLogin(jsonParam);
            HashMap<String, Object> map = new HashMap<>();
            map.put("token",token);
            if (ObjectUtil.isNotEmpty(token)){
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            log.error("用户登录出错"+e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * 从token中获取用户信息
     * @param
     * @return
     */
    @PostMapping("users/profile")
    public ResponseEntity<BackgroundUserVo> backgroundInfo( @RequestHeader(value = "Authorization") String token){

        try {

            BackgroundUserVo backgroundUserVo = backstageLoginService.getBackgroundInfo(token);
            if (ObjectUtil.isNotEmpty(backgroundUserVo)){
                return ResponseEntity.ok(backgroundUserVo);
            }

        } catch (Exception e) {
            log.error("用户基本信息出错"+e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * 退出登录
     * @param token
     * @return
     */

    @PostMapping("users/logout")
    public ResponseEntity<Void> backgroundLogout(@RequestHeader(value = "Authorization") String token){
        try {

            backstageLoginService.backgroundLogout(token);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            log.error("用户登出出错"+e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

}
